#!/usr/bin/env python
#coding=utf-8

import sys
reload(sys)
sys.setdefaultencoding('utf8')

from uniform_encode_decode import uniform_encode, uniform_decode

class post_detail_item(object):
	'''
		page_type: 
		    "CSDN_PAGE_TYPE_UNKNOWN"
		    "CSDN_PAGE_TYPE_EXPERT"
		    "CSDN_PAGE_TYPE_POST_LIST"
	    	    "CSDN_PAGE_TYPE_POST_DETAIL"
	            "CSDN_PAGE_TYPE_AUTHOR_HOME"

		author_name,
		articles_url
		home_url,

		post_title,
		post_url,
		post_date,
		post_tags,
		post_abstract,[no]
		post_content,

	'''
	def __init__(self):
		self.d = {}

	def add(self, k, v):
		self.d[k] = v

	def has(self, k):
		return k in self.d

	def value(self, k):
		assert has(k), 'no key exist'
		return self.d[k]

	def toJson(self):
		json_list = []
		# for 比较通用，但是不容易跟踪
		#for (k, v) in self.d.items():
		#	json_list.append('"%s": "%s"' % (k, uniform_encode(str(v))))

		item = 'author_name'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, self.d[item]))

		item = 'home_url'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, self.d[item]))

		item = 'articles_url'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, self.d[item]))

		item = 'post_title'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, uniform_encode(self.d[item])))

		item = 'post_url'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, self.d[item]))

		item = 'post_date'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, self.d[item]))

		item = 'post_tags'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, uniform_encode(self.d[item])))

		item = 'post_abstract'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, uniform_encode(self.d[item])))

		item = 'post_content'
		if item in self.d:
			json_list.append('"%s": "%s"' % (item, uniform_encode(self.d[item])))

		return '{%s}' % ','.join(json_list)

	def fromJson(self, json_str):
		assert False, '----not implement----'
		pass

	def __str__(self):
		str = ""
		#for (k, v) in self.d.items():
		#	str += "%s: %s\n" % (k, v)

		item = 'author_name'
		if item in self.d:
			str += "%s: %s\n" % (item, self.d[item])

		item = 'home_url'
		if item in self.d:
			str += "%s: %s\n" % (item, self.d[item])

		item = 'articles_url'
		if item in self.d:
			str += "%s: %s\n" % (item, self.d[item])

		item = 'post_title'
		if item in self.d:
			str += "%s: %s\n" % (item, self.d[item])

		item = 'post_url'
		if item in self.d:
			str += "%s: %s\n" % (item, self.d[item])

		item = 'post_tags'
		if item in self.d:
			str += "%s: %s\n" % (item, self.d[item])

		item = 'post_date'
		if item in self.d:
			str += "%s: %s\n" % (item, self.d[item])
		
		item = 'post_abstract'
		if item in self.d:
			str += "%s: %s\n" % (item, self.d[item])

		item = 'post_content'
		if item in self.d:
			#str += "%s: %s\n" % (item, self.d[item])
			str += "%s: %s\n" % (item, '...ignore...(refer PostDetailItem::__str__)')

		return str

	def __len__(self):
		return len(self.d)
